﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SemiCASE.Business;
using System.Data;

public partial class PopUp_WorkItemList : SemiCASEBasePage
{
    private string ProjectID
    {
        get
        {
            return Request.QueryString["ProjectID"] != null ? Request.QueryString["ProjectID"] : String.Empty;
        }
    }

    private string UseCaseID
    {
        get
        {
            return Request.QueryString["UseCaseID"] != null ? Request.QueryString["UseCaseID"] : String.Empty;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadUseCaseName();
            LoadWorkItems();
        }
    }

    private void LoadUseCaseName()
    {
        DataTable dt = (DataTable)Session["UseCaseList"];
        if (dt == null)
            return;

        DataRow[] rows = dt.Select("UseCaseID='" + this.UseCaseID + "'");
        LabelUcName.Text = rows[0]["Name"].ToString();
    }

    protected void ButtonYeni_Click(object sender, EventArgs e)
    {
        Session["WorkItemListCount"] = gvWorkItem.Rows.Count;
        Response.Redirect("CreateWorkItem.aspx?ProjectID=" + this.ProjectID + "&UseCaseID=" + this.UseCaseID + "&ScreenMode=Create");
    }

    private void LoadWorkItems()
    {
        DataTable dt = new WorkItemBS().LoadWorkItemFromDB(new Guid(this.ProjectID), new Guid(this.UseCaseID));

        if (dt == null || dt.Rows.Count == 0)
        {
            gvWorkItem.DataSource = null;
            gvWorkItem.DataBind();
        }
        else
        {
            gvWorkItem.DataSource = dt;
            gvWorkItem.DataBind();
        }
    }

    protected void gvWorkItem_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteWorkItem")
        {
            DeleteWorkItem(e.CommandArgument.ToString());
        }
        else if (e.CommandName == "UpdateWorkItem")
        {
            Response.Redirect("CreateWorkItem.aspx?ProjectID=" + this.ProjectID + "&UseCaseID=" + this.UseCaseID + "&WorkItemID=" + e.CommandArgument.ToString() + "&ScreenMode=Update");
        }
    }

    private void DeleteWorkItem(string WorkItemID)
    {
        new WorkItemBS().DeleteWorkItem(new Guid(WorkItemID));
        LoadWorkItems();
        MessageBox("Test Case deleted successfully", SemiCASE.Interfaces.MesajTuruEnum.Bilgi);
    }
}